package net.notdot.aeauth;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
 
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;
 
public class ReadXMLUrlSAX {
    public ListaAvisos tempAvisos = new ListaAvisos();
    public ArrayList<String> tempCuando = new ArrayList<String>();
	DefaultHandler handler = new DefaultHandler() {
     String tempVal;
     Aviso tempAviso;
    
     public void startElement(String uri, String localName,
        String qName, Attributes attributes)
        throws SAXException {
    	tempVal = "";
    	
        if(qName.equalsIgnoreCase("aviso")){
        	tempAviso = new Aviso();	
        	Log.i("startElement","new tempAviso");
        	tempCuando=new ArrayList<String>();
        	Log.i("startElement","new tempCuando");
        	
        }
 
     }
 
     public void endElement(String uri, String localName,
          String qName)
          throws SAXException {
    	 
         if (qName.equalsIgnoreCase("aviso")) {
        	 tempAviso.setCuando(tempCuando);
        	 Log.i("endElement","setCuando");
            tempAvisos.add(tempAviso);
            Log.i("endElement","add(tempAviso)");
          }
         else if(qName.equalsIgnoreCase("id")){
         	tempAviso.setId(tempVal);  
         	Log.i("endElement Actividad ",tempVal);
           }
         else if(qName.equalsIgnoreCase("actividad")){
        	tempAviso.setActividad(tempVal);  
        	Log.i("endElement Actividad ",tempVal);
          }
         else if(qName.equalsIgnoreCase("cuanto")){
         	tempAviso.setCuanto(tempVal);  
         	Log.i("endElement Cuanto ",tempVal);
         }
         else if(qName.equalsIgnoreCase("cuando")){
         	tempCuando.add(tempVal);  
         	Log.i("endElement Cuando ",tempVal);
         }
         else if(qName.equalsIgnoreCase("autor")){
         	tempAviso.setAutor(tempVal);  
         	Log.i("endElement Autor ",tempVal);
         }
         else if(qName.equalsIgnoreCase("fecha")){
         	tempAviso.setFecha(tempVal);  
         	Log.i("endElement Fecha ",tempVal);
         }
         else if(qName.equalsIgnoreCase("repeticion")){
         	tempAviso.setRepeticion(tempVal);  
         	Log.i("endElement Repeticion ",tempVal);
         }
     }
 
     public void characters(char ch[], int start, int length)
         throws SAXException {
    	 
    	 tempVal= new String(ch,start,length);         
 
        }
 
      };
      
      public ListaAvisos parseAvisos(InputStream inputStream) throws ParserConfigurationException, SAXException {
      	  
    	  SAXParserFactory factory = SAXParserFactory.newInstance();
          SAXParser saxParser = factory.newSAXParser();
		try {
	        Reader reader = new InputStreamReader(inputStream,"UTF-8");
	        InputSource inputSource = new InputSource(reader);
	        inputSource.setEncoding("UTF-8");
	        saxParser.parse(inputStream, handler);
	        
	        
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tempAvisos;

      }

}